#include "./fiipCloudListeners.h"

void listener0xFFFF(Format_FieldStruct* msg) {
  if (fiipChecking_isDuplicate(msg)) {
    msg->cmd[0] = 0x00;  // 取消后续命令
    msg->cmd[1] = 0x00;
    return;
  }

  uint8_t err = msg->errno[0];
  if (err != 0x00) {
    if (err == 0x11 || err == 0x13 || err == 0x14) {
      fiipCloud_login();
    } else {
      typedef void(FUN)(uint8_t err);
      FUN* fun;
      fun = (FUN*)fiipCloud_getEmitter(CloudEvent_error);
      if (fun)
        fun(err);
    }
    msg->cmd[0] = 0x00;  // 取消后续命令
    msg->cmd[1] = 0x00;
  }
};

void listener0xF011(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t key[4];

  mem_cpy(id, &msg->body[0], 8);
  mem_cpy(key, &msg->body[8], 4);

  typedef void(FUN)(uint8_t * id, uint8_t * key);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_getId);
  if (fun)
    fun(id, key);
};
void listener0xF012(Format_FieldStruct* msg) {
  typedef void(FUN)();
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_login);
  if (fun)
    fun();
};
void listener0xF013(Format_FieldStruct* msg) {
  typedef void(FUN)();
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_heart);
  if (fun)
    fun();
};
void listener0xF014(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t key[4];

  mem_cpy(id, &msg->body[0], 8);
  mem_cpy(key, &msg->body[8], 4);

  typedef void(FUN)(uint8_t * id, uint8_t * key);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_bind);
  if (fun)
    fun(id, key);
};
void listener0xF015(Format_FieldStruct* msg) {
  typedef void(FUN)();
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_unbind);
  if (fun)
    fun();
};
void listener0xF016(Format_FieldStruct* msg) {
  uint8_t key[4];

  mem_cpy(key, &msg->body[0], 4);

  typedef void(FUN)(uint8_t * key);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_enterBinding);
  if (fun)
    fun(key);
};
void listener0x7811(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t key[4];

  mem_cpy(id, &msg->body[0], 8);
  mem_cpy(key, &msg->body[8], 4);

  typedef void(FUN)(uint8_t * id, uint8_t * key);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_beBound);
  if (fun)
    fun(id, key);

  fiip_respond(fiipCloud_txd, msg, 0x00, NULL, 0);
};
void listener0x7812(Format_FieldStruct* msg) {
  uint8_t id[8];

  mem_cpy(id, &msg->body[0], 8);

  typedef void(FUN)(uint8_t * id);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_beUnbound);
  if (fun)
    fun(id);

  fiip_respond(fiipCloud_txd, msg, 0x00, NULL, 0);
};

void listener0xF021(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t kLen = msg->body[8];
  uint8_t vLen = msg->body[9 + kLen];

  mem_cpy(id, &msg->body[0], 8);
  fiipCloudVar_setActual(id, &msg->body[9], kLen, &msg->body[10 + kLen], vLen);
};
void listener0xF022(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t kLen = msg->body[8];
  uint8_t vLen = msg->body[9 + kLen];

  mem_cpy(id, &msg->body[0], 8);
  fiipCloudVar_setActual(id, &msg->body[9], kLen, &msg->body[10 + kLen], vLen);
};
void listener0xF023(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t kLen = msg->body[8];

  mem_cpy(id, &msg->body[0], 8);

  typedef void(FUN)(uint8_t * id, uint8_t * kVal, uint8_t kLen);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_subActualVar);
  if (fun)
    fun(id, &msg->body[9], kLen);
};
void listener0xF024(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t i = 8;

  mem_cpy(id, &msg->body[0], 8);
  while (i < ((msg->len[0] << 8) | msg->len[1])) {
    fiipCloudVar_setActual(id, &msg->body[i + 1], msg->body[i], NULL, 0);
    i = i + 1 + msg->body[i];
  }

  typedef void(FUN)(uint8_t * id);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_getActualAllVar);
  if (fun)
    fun(id);
};
void listener0xF025(Format_FieldStruct* msg) {
  uint8_t id[8];

  mem_cpy(id, &msg->body[0], 8);

  typedef void(FUN)(uint8_t * id);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_subActualAllVar);
  if (fun)
    fun(id);
};
void listener0xF029(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t kLen = msg->body[8];
  uint8_t vLen = msg->body[9 + kLen];

  mem_cpy(id, &msg->body[0], 8);
  fiipCloudVar_setTarget(id, &msg->body[9], kLen, &msg->body[10 + kLen], vLen);
};
void listener0xF02A(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t kLen = msg->body[8];
  uint8_t vLen = msg->body[9 + kLen];

  mem_cpy(id, &msg->body[0], 8);
  fiipCloudVar_setTarget(id, &msg->body[9], kLen, &msg->body[10 + kLen], vLen);
};
void listener0xF02B(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t kLen = msg->body[8];

  mem_cpy(id, &msg->body[0], 8);

  typedef void(FUN)(uint8_t * id, uint8_t * kVal, uint8_t kLen);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_subTargetVar);
  if (fun)
    fun(id, &msg->body[9], kLen);
};
void listener0xF02C(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t i = 8;

  mem_cpy(id, &msg->body[0], 8);
  while (i < ((msg->len[0] << 8) | msg->len[1])) {
    fiipCloudVar_setTarget(id, &msg->body[i + 1], msg->body[i], NULL, 0);
    i = i + 1 + msg->body[i];
  }

  typedef void(FUN)(uint8_t * id);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_getTargetAllVar);
  if (fun)
    fun(id);
};
void listener0xF02D(Format_FieldStruct* msg) {
  uint8_t id[8];

  mem_cpy(id, &msg->body[0], 8);

  typedef void(FUN)(uint8_t * id);
  FUN* fun;
  fun = (FUN*)fiipCloud_getEmitter(cloudEvent_subTargetAllVar);
  if (fun)
    fun(id);
};
void listener0x7821(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t kLen = msg->body[8];
  uint8_t vLen = msg->body[9 + kLen];

  mem_cpy(id, &msg->body[0], 8);
  fiipCloudVar_setActual(id, &msg->body[9], kLen, &msg->body[10 + kLen], vLen);
  fiip_respond(fiipCloud_txd, msg, 0x00, NULL, 0);
};
void listener0x7829(Format_FieldStruct* msg) {
  uint8_t id[8];
  uint8_t kLen = msg->body[8];
  uint8_t vLen = msg->body[9 + kLen];

  mem_cpy(id, &msg->body[0], 8);
  fiipCloudVar_setTarget(id, &msg->body[9], kLen, &msg->body[10 + kLen], vLen);
  fiip_respond(fiipCloud_txd, msg, 0x00, NULL, 0);
};

void fiipCloud_startListeners(void) {
  fiipListener_add(0xffff, listener0xFFFF);
  fiipListener_add(0xf011, listener0xF011);
  fiipListener_add(0xf012, listener0xF012);
  fiipListener_add(0xf013, listener0xF013);
  fiipListener_add(0xf014, listener0xF014);
  fiipListener_add(0xf015, listener0xF015);
  fiipListener_add(0xf016, listener0xF016);
  fiipListener_add(0x7811, listener0x7811);
  fiipListener_add(0x7812, listener0x7812);
  fiipListener_add(0xf021, listener0xF021);
  fiipListener_add(0xf022, listener0xF022);
  fiipListener_add(0xf023, listener0xF023);
  fiipListener_add(0xf024, listener0xF024);
  fiipListener_add(0xf025, listener0xF025);
  fiipListener_add(0xf029, listener0xF029);
  fiipListener_add(0xf02a, listener0xF02A);
  fiipListener_add(0xf02b, listener0xF02B);
  fiipListener_add(0xf02c, listener0xF02C);
  fiipListener_add(0xf02d, listener0xF02D);
  fiipListener_add(0x7821, listener0x7821);
  fiipListener_add(0x7829, listener0x7829);
}
